package main;

import javax.swing.*;
import storage.*;
import entity.*;
import java.awt.*;
import java.awt.event.*;

public class EditUserDialog extends JDialog
{	
	private JButton okButton = new JButton("OK");
	private JLabel columnLabel = new JLabel("Username :");
	private JTextField usernameInput = new JTextField(10);
	private JPasswordField passwordInput = new JPasswordField(10);
	private String usr = "";
	private final JLabel lblDataType = new JLabel("Password :");
	private JPanel pane = new JPanel();
	private UserAccessManager data;
	private ManagementFrame parentFrame;
	
	Font fon = new Font("Dialog",Font.PLAIN,15);
	private final JLabel lblNewUser = new JLabel("New User");
	private final JButton addButton = new JButton("Add");
	private final JButton removeButton = new JButton("Remove");
	private final JLabel lblUserList = new JLabel("User List");
	private final JList list = new JList();
	private final DefaultListModel listModel = new DefaultListModel();
	
	
	
	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy)
	{
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane()
	{
		GridBagLayout gridbag = new GridBagLayout();
		gridbag.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0};
		gridbag.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		buildConstraints(constraints,0,0,3,1,1,1);
		
		buildConstraints(constraints,3,0,3,1,1,1);
		
		GridBagConstraints gbc_lblNewUser = new GridBagConstraints();
		gbc_lblNewUser.anchor = GridBagConstraints.SOUTH;
		gbc_lblNewUser.gridwidth = 2;
		gbc_lblNewUser.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewUser.gridx = 0;
		gbc_lblNewUser.gridy = 0;
		pane.add(lblNewUser, gbc_lblNewUser);
		
		GridBagConstraints gbc_lblUserList = new GridBagConstraints();
		gbc_lblUserList.insets = new Insets(0, 0, 5, 0);
		gbc_lblUserList.gridx = 3;
		gbc_lblUserList.gridy = 0;
		pane.add(lblUserList, gbc_lblUserList);
		gridbag.setConstraints(columnLabel, constraints);
		GridBagConstraints gbc_dbLabel = new GridBagConstraints();
		gbc_dbLabel.insets = new Insets(0, 0, 5, 5);
		gbc_dbLabel.gridx = 0;
		gbc_dbLabel.gridy = 1;
		pane.add(columnLabel, gbc_dbLabel);

		buildConstraints(constraints,0,2,2,1,1,1);

		buildConstraints(constraints,2,2,2,1,1,1);

		setLocation(200,80);
		setContentPane(pane);
		gridbag.setConstraints(usernameInput, constraints);
		usernameInput.setFont(fon);
		GridBagConstraints gbc_dbnameInput = new GridBagConstraints();
		gbc_dbnameInput.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbnameInput.insets = new Insets(0, 0, 5, 5);
		gbc_dbnameInput.gridy = 1;
		gbc_dbnameInput.gridx = 1;
		pane.add(usernameInput, gbc_dbnameInput);
		
		
		GridBagConstraints gbc_addButton = new GridBagConstraints();
		gbc_addButton.insets = new Insets(0, 0, 5, 5);
		gbc_addButton.gridx = 2;
		gbc_addButton.gridy = 1;
		pane.add(addButton, gbc_addButton);
		
		GridBagConstraints gbc_list = new GridBagConstraints();
		gbc_list.gridheight = 2;
		gbc_list.insets = new Insets(0, 0, 5, 0);
		gbc_list.fill = GridBagConstraints.BOTH;
		gbc_list.gridx = 3;
		gbc_list.gridy = 1;
		pane.add(list, gbc_list);
		
		
		GridBagConstraints gbc_dbpasswordInput = new GridBagConstraints();
		gbc_dbpasswordInput.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbpasswordInput.insets = new Insets(0, 0, 5, 5);
		gbc_dbpasswordInput.gridy = 2;
		gbc_dbpasswordInput.gridx = 1;
		pane.add(passwordInput, gbc_dbpasswordInput);
		
		GridBagConstraints gbc_lblDataType = new GridBagConstraints();
		gbc_lblDataType.insets = new Insets(0, 0, 5, 5);
		gbc_lblDataType.gridx = 0;
		gbc_lblDataType.gridy = 2;
		pane.add(lblDataType, gbc_lblDataType);
		
		GridBagConstraints gbc_removeButton = new GridBagConstraints();
		gbc_removeButton.insets = new Insets(0, 0, 5, 5);
		gbc_removeButton.gridx = 2;
		gbc_removeButton.gridy = 2;
		pane.add(removeButton, gbc_removeButton);
		
		gridbag.setConstraints(okButton, constraints);
		GridBagConstraints gbc_okButton = new GridBagConstraints();
		gbc_okButton.gridwidth = 4;
		gbc_okButton.gridx = 0;
		gbc_okButton.gridy = 3;
		pane.add(okButton, gbc_okButton);
	}
	
	private void initListBox()
	{
		list.setModel(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		for (int i=0;i<data.userList.size();i++)
		{
			listModel.addElement(data.userList.get(i).getUsername());
		}
	}

	private void initListener()
	{
		ActListener actListener = new ActListener();
		okButton.addActionListener(actListener);
		addButton.addActionListener(actListener);
		removeButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == okButton) okRun();
			if (e.getSource() == addButton) addRun();
			if (e.getSource() == removeButton) removeRun();
		}
	}
	
	
	private void addRun()
	{
		String password =  new String(passwordInput.getPassword());
		if (usernameInput.getText().compareTo("")!=0 && password.compareTo("")!=0)
		{
			data.addUser(new User(usernameInput.getText(),password));
			listModel.addElement(usernameInput.getText());
			usernameInput.setText("");
			passwordInput.setText("");
			data.saveUsers();
		}
		else 
		{
			JOptionPane.showMessageDialog(null, "Please enter username and password.");
		}
	}
	private void removeRun()
	{
		if (list.getSelectedIndex()!=-1)
		{
			data.deleteUserByName(listModel.getElementAt(list.getSelectedIndex()).toString());
			listModel.removeElementAt(list.getSelectedIndex());
			data.saveUsers();
		}
	}
	
	private void okRun()
	{
		this.dispose();
	}
	public EditUserDialog(ManagementFrame frame)
	{
		data = new UserAccessManager();
		parentFrame = frame;
		setTitle("Edit Column");
		setModal(true);
		setSize(433,178);
		initListener();
		setMyPane();
		initListBox();
		setVisible(true);
	}
}
